
package fpt.pits.portlet;

import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import java.util.List;

import fpt.pits.blo.LoginInfoBLO;
import fpt.pits.form.LoginInfoForm;
import fpt.pits.util.Constants;

/**
 * LoginInfoPortlet Portlet Class
 * @author yoyosakura
 */
public class LoginInfoPortlet extends GenericPortlet {
    /**
     * loginForm property
     */
    private LoginInfoForm loginForm = null;
    /**
     * loginBLO property
     */
    private LoginInfoBLO loginBLO = null;
    /**
     * action property
     */
    private String action = Constants.BLANK_VALUE;
    /**
     * flag property
     */
    @SuppressWarnings("unused")
    private boolean flag = true;
	public void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		PortletRequestDispatcher  dispatcher = null;
		
		if (request.getParameter("project") != null) {
		    request.getPortletSession().setAttribute(Constants.INDEX_RIGHT_PORT_SESS_ATT, 
		            Constants.EDIT_PROJECT_INFO_PAGE, PortletSession.APPLICATION_SCOPE);
		}
		if (request.getParameter("employee") != null) {
		    request.getPortletSession().setAttribute(Constants.INDEX_RIGHT_PORT_SESS_ATT, 
                    Constants.EDIT_EMPLOYEE_INFO_PAGE, PortletSession.APPLICATION_SCOPE);
		}
		
		if (!action.equals(Constants.LOGIN_ACTION)) {
		    dispatcher = getPortletContext().getRequestDispatcher(Constants.LOGIN_VIEW_PATH);
	        dispatcher.include(request, response);
		} else {
		    List<String> errorList = loginForm.validate(request, response);
		    if (errorList.isEmpty()) {
		        loginBLO = new LoginInfoBLO();
		        if (loginBLO.isValidUser(loginForm.getTxtUsername(), loginForm.getTxtPassword())) {
		            //
		            request.getPortletSession().setAttribute(Constants.INDEX_LEFT_PORT_SESS_ATT, 
		                    Constants.MENU_PAGE, PortletSession.APPLICATION_SCOPE);
		            //
		            request.getPortletSession().setAttribute(Constants.USERNAME_PORT_SESS_ATT, 
		                    loginForm.getTxtUsername(), PortletSession.APPLICATION_SCOPE);
		            dispatcher = getPortletContext().getRequestDispatcher(Constants.INDEX_VIEW_PATH);
		            dispatcher.include(request, response);
		        } else {
		            request.getPortletSession().setAttribute(Constants.ERROR_MESSAGES_INVALID_PORT_SESS_ATT, 
		                    "Invalid username and password! Please try again.", PortletSession.APPLICATION_SCOPE);
		            request.getPortletSession().removeAttribute(Constants.USERNAME_PORT_SESS_ATT);
		            dispatcher = getPortletContext().getRequestDispatcher(Constants.LOGIN_VIEW_PATH);
                    dispatcher.include(request, response);
		        }
		    } else {
		        request.getPortletSession().setAttribute(Constants.ERROR_MESSAGES_INVALID_PORT_SESS_ATT, 
		               errorList.get(0), PortletSession.APPLICATION_SCOPE);
		        dispatcher = getPortletContext().getRequestDispatcher(Constants.LOGIN_VIEW_PATH);
                dispatcher.include(request, response);
		    }
		}
	}

	public void processAction(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
	    flag = false;
	    loginForm = new LoginInfoForm();
	    if (request.getParameter("hidAction") != null) {
	        action = request.getParameter("hidAction").toString();
	    }
	    if (request.getParameter("txtUsername") != null) {
	        loginForm.setTxtUsername(request.getParameter("txtUsername").toString());
	    }
	    if (request.getParameter("txtPassword") != null) {
	        loginForm.setTxtPassword(request.getParameter("txtPassword").toString());
	    }
	}

}
